﻿using DevelopTool.Bll.OffLineBlls;
using DevelopTool.Common;
using DevelopTool.Forms.CommonForms;
using DevelopTool.Model;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.Enums;
using DevelopTool.Model.OffLineModels;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace DevelopTool.Forms.CommonHelpers
{
    public class OffLineHelper : BaseHelper
    {
        /// <summary>
        /// 保存DataTable到离线数据
        /// </summary>
        /// <param name="dataTable"></param>
        /// <param name="dataGridColumns"></param>
        public static void SaveToOffLine(DataTable dataTable, List<DataGridColumn> dataGridColumns)
        {
            if (dataTable == null || dataTable.Rows.Count <= 0) return;

            IgnoringColumns(dataTable);

            OffLineBll offLineBll = new OffLineBll();

            ChoiceMenuForm choiceMenuForm = new ChoiceMenuForm("保存为离线数据", "离线数据" + DateTime.Now.DateTimeString(false), offLineBll.GetList(MenuNodeType.Folder).Cast<ManageMenu>());
            choiceMenuForm.MenuAdd += delegate (object ss, MenuEventArgs ee)
            {
                OffLine offLine = new OffLine()
                {
                    NodeName = ee.ManageMenu.NodeName,
                    NodeType = ee.ManageMenu.NodeType,
                    NodePid = ee.ManageMenu.NodePid,
                };

                if (ee.ManageMenu.NodeType == MenuNodeType.Node)//只保存在节点上
                {
                    offLine.DataTable = dataTable;
                    offLine.DataGridColumns = dataGridColumns;
                }

                ee.ManageMenu = offLineBll.Save(offLine);

                UINotifierHelper.ShowNotifier($"【{offLine.NodeName}】保存成功", UINotifierType.INFO, UIStyles.CurrentResources.InfoTitle, false, 2000);
            };

            choiceMenuForm.ShowDialog();
        }

    }
}
